﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Data;
using System.Linq;

#endregion

namespace ScrumTable.Common.Data
{
    /// <summary>
    /// Provides the additional functionalities required for ADO.NET DataTable 
    /// management.
    /// </summary>
    public static class DataTableUtil
    {
        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Retrieves the unique id column of the current table.
        /// </summary>
        /// <param name="thisPtr">Specifies the pointer to the DataTable instance.</param>
        public static bool HasPrimaryKey(this DataTable thisPtr)
        {
            PreCondition.AssertNotNull(thisPtr, "thisPtr");

            return (thisPtr.PrimaryKey != null && thisPtr.PrimaryKey.Length > 0);
        }

        /// <summary>
        /// Gets a typed enumerable of DataRows.
        /// </summary>
        /// <param name="thisPtr">Specifies the pointer to the DataTable instance.</param>
        public static IEnumerable<DataRow> GetRows(this DataTable thisPtr)
        {
            PreCondition.AssertNotNull(thisPtr, "thisPtr");
            return thisPtr.Rows.Cast<DataRow>();
        }

        /// <summary>
        /// Gets a typed enumerable of DataColumns.
        /// </summary>
        /// <param name="thisPtr">Specifies the pointer to the DataTable instance.</param>
        public static IEnumerable<DataColumn> GetColumns(this DataTable thisPtr)
        {
            PreCondition.AssertNotNull(thisPtr, "thisPtr");
            return thisPtr.Columns.Cast<DataColumn>();
        }

        #endregion
    }
}